<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="apple-mobile-web-app-capable" content="yes" />
        <meta name="apple-mobile-web-app-status-bar-style" content="black" />
        <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1.0, maximum-scale=1.0, minimal-ui" />
        
        <title>Public API Documentation</title>
        
        <script src="/js/greyspots.js" type="text/javascript"></script>
        <link href="/css/greyspots.css" type="text/css" rel="stylesheet" />
        
        <script src="/js/ace/ace.js" data-ace-base="/js/ace/" type="text/javascript" charset="utf-8"></script>
        <script src="/js/ace/ext-language_tools.js" type="text/javascript"></script>
        <script src="/js/ace/ext-searchbox.js" type="text/javascript"></script>
        
        <script src="doc-library/doc.js" type="text/javascript"></script>
        <link href="doc-library/doc.css" type="text/css" rel="stylesheet" />
    </head>
    <body>
        <gs-jumbo>
            <h1 class="text-center">Public</h1>
        </gs-jumbo>
            
        <gs-container min-width="sml;med;lrg">
            <h3>URLs:</h3>
            <pre><code>/env/[&lt;SCHEMA&gt;.]&lt;FUNCTION&gt;[?&lt;ARGS&gt;]</code></pre>
            
            <h3>Description:</h3>
            <p>These links handle users with no cookie. If they do not work, you need to enable No Cookie requests in the envelope.conf.</p>
            <p>NC stands for No Cookie. Only functions that start with actionnc_ or acceptnc_ are allowed.</p>
            <p>Envelope provides context info to postgres for functions, access using:</p>
            <code>
            current_setting('sunny.context_info')
            </code>
            <p>This returns something like:</p>
            <pre>request_ip_address=127.0.0.1&request_host=127.0.0.1%3A8082</pre>
            
            <h1>Examples:</h1>
            
            <div class="doc-example-description">
                <span class="h3">Action No Cookie Example:</span><br/>
                <p>Actionnc_ functions return json.</p>
            </div>
            <gs-doc-example>
                <template for="html" height="5">
                    <gs-button onclick="ajax_example()">Action</gs-button>
                    <pre id="result"></pre>
                </template>
                <template for="js" height="20">
                    function ajax_example() {
                        GS.addLoader('loader', 'Ajax running...');
                        GS.ajaxJSON('/env/gsdoc.actionnc_test', 'test=test'
                            , function (data, error) {
                            GS.removeLoader('loader');
                            
                            if (!error) {
                                document.getElementById('result').textContent = JSON.stringify(data);
                            } else {
                                GS.ajaxErrorDialog(data);
                            }
                        });
                    }
                </template>
                <template for="db" height="30">
                    CREATE OR REPLACE FUNCTION gsdoc.actionnc_test(str_args text)
                     RETURNS text AS
                    $BODY$
                    BEGIN
                        --You can get the ip address and host fields from the request. Since you don't know who logged in you can use these headers to determine who is who, but not as reliably as a cookie. This also available on cookie requests, but is not as useful.
                        RETURN
                            '{"context_info": "' || current_setting('sunny.context_info') || '"}';
                        --try all of these
                        --RETURN '123';
                        --RETURN array_to_json(ARRAY[[str_args]]::text[][]);
                        --RETURN '"test"';
                        --RETURN '{"test": "test", "test2": 123}';
                    END; 
                    
                    $BODY$
                    LANGUAGE plpgsql VOLATILE
                    COST 100;
                    ALTER FUNCTION gsdoc.actionnc_test(text) OWNER TO postgres;
                    GRANT ALL ON FUNCTION gsdoc.actionnc_test(text) TO public_g;
                    REVOKE ALL ON FUNCTION gsdoc.actionnc_test(text) FROM public;
                </template>
            </gs-doc-example>
            
            <div class="doc-example-description">
                <span class="h3">Accept No Cookie Example:</span><br/>
                <p>Acceptnc functions return a full response.</p>
            </div>
            <gs-doc-example>
                <template for="html" height="5">
                    <gs-button onclick="ajax_example()">Accept</gs-button>
                    <pre id="result"></pre>
                </template>
                <template for="js" height="20">
                    function ajax_example() {
                        GS.addLoader('loader', 'Ajax running...');
                        GS.ajaxText(
                                '/env/gsdoc.acceptnc_test'
                              , 'test=test'
                              , function (data, error) {
                            GS.removeLoader('loader');
                            
                            if (!error) {
                                document.getElementById('result').textContent = data;
                            } else {
                                GS.ajaxErrorDialog(data);
                            }
                        });
                    }
                </template>
                <template for="db" height="25">
                    CREATE OR REPLACE FUNCTION gsdoc.acceptnc_test(str_args text)
                     RETURNS text AS
                    $BODY$
                    BEGIN
                        RETURN E'HTTP/1.1 200 OK\r\n' ||
                          E'Content-Type: text/csv\r\n\r\n' ||
                          E'Header1, Header2, Header3\r\n' ||
                          E'Data11, Data12, Data13\r\n' ||
                          E'Data21, Data22, Data23\r\n' ||
                          E'Data31, Data32, ' || str_args || '\r\n';
                    END; 
                    
                    $BODY$
                    LANGUAGE plpgsql VOLATILE
                    COST 100;
                    ALTER FUNCTION gsdoc.acceptnc_test(text) OWNER TO postgres;
                    GRANT ALL ON FUNCTION gsdoc.acceptnc_test(text) TO public_g;
                    REVOKE ALL ON FUNCTION gsdoc.acceptnc_test(text) FROM public;
                </template>
            </gs-doc-example>
            
            <div class="doc-example-description">
                <span class="h3">Accept Download No Cookie Example:</span><br/>
                <p>When you want to download a file from the database, sometimes you will want a custom file name. Simply add the filename after the schema.function. That path will be sent to the function as path=/file.txt.</p>
            </div>
            <gs-doc-example>
                <template for="html">
                    <form id="form_example" method="post" action="/env/gsdoc.acceptnc_test/file.txt">
                        <input type="hidden" name="test" value="test"/>
                    </form>
                    <gs-button onclick="document.getElementById('form_example').submit();">Accept</gs-button>
                </template>
                <template for="db" height="25">
                    CREATE OR REPLACE FUNCTION gsdoc.acceptnc_test(str_args text)
                     RETURNS text AS
                    $BODY$
                    BEGIN
                        RETURN E'HTTP/1.1 200 OK\r\n' ||
                          E'Content-Type: text/csv\r\n\r\n' ||
                          E'Header1, Header2, Header3\r\n' ||
                          E'Data11, Data12, Data13\r\n' ||
                          E'Data21, Data22, Data23\r\n' ||
                          E'Data31, Data32, ' || str_args || '\r\n';
                    END; 
                    
                    $BODY$
                    LANGUAGE plpgsql VOLATILE
                    COST 100;
                    ALTER FUNCTION gsdoc.acceptnc_test(text) OWNER TO postgres;
                    GRANT ALL ON FUNCTION gsdoc.acceptnc_test(text) TO normal_g;
                    REVOKE ALL ON FUNCTION gsdoc.acceptnc_test(text) FROM public;
                </template>
            </gs-doc-example>
        </gs-container>
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
    </body>
</html>